package com.achcorp.sarch.core.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.achcorp.sarch.core.domain.Document;
import com.achcorp.sarch.core.domain.Tag;

public class DocumentDAOImp implements DocumentDAO {
	private HibernateTemplate hibernateTemplate;
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@SuppressWarnings("unchecked")
	public List<Document> getAllDocuments() {
		List<Document> l = null;
		Session session = null;
		try {

			session = sessionFactory.openSession();
			l = session.createQuery("from Document").list();
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return l;
	}

	public Document getDocument(long id) {

		Document c = hibernateTemplate.get(Document.class, id);
		return c;
	}

	public void addDocument(Document doc) {

		Session session = null;
		try {

			session = sessionFactory.openSession();
			Transaction tr = session.beginTransaction();
			List<Tag> tags = doc.getTags();
			addTags(tags, session);
			session.saveOrUpdate(doc);
			tr.commit();

			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void addTags(List<Tag> tags, Session session) {
		for (Tag tag : tags) {
			if (tag.getParent() != null) {
				session.saveOrUpdate(tag.getParent());
			}
			session.saveOrUpdate(tag);
		}

	}

	public void updateDocument(Document doc) {
		/*
		 * String insertString1, insertString2, insertString3, insertString4;
		 * insertString1 = "insert into documents values('', " +
		 * doc.getProperties().get(0).getName() + ")";
		 * 
		 * Statement stmt; try { stmt = getConnection().createStatement();
		 * stmt.executeUpdate(insertString1); } catch (SQLException e) {
		 * e.printStackTrace(); }
		 */

		Session session = null;
		try {

			session = sessionFactory.openSession();
			Transaction tr = session.beginTransaction();
			session.saveOrUpdate(doc);
			tr.commit();

			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/*
	 * public List<String> getAllProperties() { // TODO Auto-generated method
	 * stub
	 * 
	 * Session session = null; try {
	 * 
	 * session = sessionFactory.openSession(); Transaction tr =
	 * session.beginTransaction();
	 * 
	 * @SuppressWarnings("unchecked") List<String> list = session.createQuery(
	 * "select distinct  name from DocProperty").list();
	 * 
	 * tr.commit();
	 * 
	 * session.close(); return list; } catch (Exception e) {
	 * e.printStackTrace(); } return null; }
	 */
	public List<Document> getDocs(String docName, String description,
			Date minDate, Date maxDate) {
		// TODO Auto-generated method stub
		Session session = null;
		try {

			session = sessionFactory.openSession();

			Transaction tr = session.beginTransaction();
			StringBuilder query = new StringBuilder();

			/*
			 * Document doc = new Document(); Set<DocProperty> se = new
			 * HashSet<DocProperty>(); se.add(new DocProperty(null, null, "v1",
			 * doc)); doc.setProperties(se); Example example =
			 * Example.create(doc)
			 * .enableLike(MatchMode.ANYWHERE).excludeZeroes() .ignoreCase();
			 * 
			 * List<Document> result = session.createCriteria(Document.class)
			 * .add(example).list();
			 */
			// session.saveOrUpdate(doc);

			// result =
			// session.createCriteria(Document.class).createCriteria("properties")props.add(Restrictions.like("value",
			// "v1")).list();

			tr.commit();

			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
}
